#ifndef ATCODER_FENWICKTREE_HPP
#define ATCODER_FENWICKTREE_HPP 1

#include <cassert>
#include <vector>

#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP
#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T>
using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T>
using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_INTERNAL_TYPE_TRAITS_HPP

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T>
struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

   public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

   private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder

#endif  // ATCODER_FENWICKTREE_HPP

#include <bits/stdc++.h>
namespace scd {
using namespace std;
template <typename Raw = int, typename T = decay_t<Raw>>
inline auto read() -> enable_if_t<is_integral<T>::value, T> {
    T x = 0;
#ifdef USE_IOSTREAM
    cin >> x;
#else
    bool fl = false;
    char c = getchar();
    while (c < '0' || c > '9')
        fl |= (c == '-'), c = getchar();
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
    return fl ? -x : x;
#endif
    return x;
}
template <typename Raw, typename T = decay_t<Raw>>
inline auto read() -> enable_if_t<!is_integral<T>::value, T> {
    T x;
    cin >> x;
    return x;
}
template <typename Raw, typename T = decay_t<Raw>>
inline auto read(Raw&& x) -> enable_if_t<is_integral<T>::value> {
#ifdef USE_IOSTREAM
    cin >> x;
#else
    x = 0;
    bool fl = false;
    char c = getchar();
    while (c < '0' || c > '9')
        fl |= (c == '-'), c = getchar();
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
#endif
}
inline void read(char* p) {
#ifdef USE_IOSTREAM
    cin >> p;
#else
    char c = getchar();
    while (isspace(c))
        c = getchar();
    while (c != EOF && !isspace(c)) {
        *p++ = c;
        c = getchar();
    }
    *p = '\0';
#endif
}
template <typename T>
inline auto read(T&& x) -> enable_if_t<!is_integral<decay_t<T>>::value> {
    cin >> x;
}
template <typename... T>
inline void read(T&&... x) {
    (read(forward<T>(x)), ...);
}
template <typename Raw = int, typename T = decay_t<Raw>>
inline auto print(Raw x) -> enable_if_t<is_integral<T>::value> {
#ifdef USE_IOSTREAM
    cout << x;
#else
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        print(x / 10);
    putchar(x % 10 + '0');
#endif
}
template <int d = 6, typename Raw = double, typename T = decay_t<Raw>>
inline auto print(Raw x) -> enable_if_t<is_floating_point<T>::value> {
#ifdef USE_IOSTREAM
    cout.precision(d);
    cout << std::fixed << x;
#else
    printf("%.*f", d, x);
#endif
}
inline void print(char x) {
#ifdef USE_IOSTREAM
    cout.put(x);
#else
    putchar(x);
#endif
}
inline void print(const char* x) {
#ifdef USE_IOSTREAM
    cout << x;
#else
    while (*x)
        putchar(*x++);
#endif
}
template <typename Raw, typename T = decay_t<Raw>>
inline auto print(Raw&& x)
    -> enable_if_t<!is_integral<T>::value && !is_floating_point<T>::value> {
    cout << x;
}
template <typename... T>
inline void print(T&&... x) {
    (print(forward<T>(x)), ...);
}
template <typename... T>
inline void printws(T&&... x) {
    (print(forward<T>(x), ' '), ...);
}
template <typename... T>
inline void println(T&&... x) {
    printws(x...);
    print('\n');
}
}  // namespace scd
using namespace scd;
namespace io {
const int L = 1 << 20;
inline char gc() {
    static char buf[L], *l = buf, *r = buf;
    if (l == r)
        r = (l = buf) + fread(buf, 1, L, stdin);
    return (l == r) ? EOF : *l++;
}
char obuf[L], *p = obuf;
inline void pc(char c) {
    if (p == obuf + L)
        fwrite(obuf, 1, L, stdout), p = obuf;
    *p++ = c;
}
inline void flush() {
    fwrite(obuf, 1, p - obuf, stdout);
}
struct Reader {
    template <typename T>
    inline Reader& operator>>(T& x) {
        x = 0;
        short f = 1;
        char c = gc();
        while (!isdigit(c)) {
            if (c == '-')
                f = -1;
            c = gc();
        }
        while (isdigit(c))
            x = 10 * x + (c - '0'), c = gc();
        x *= f;
        return *this;
    }
    Reader() {}
} in;
struct Writer {
    template <typename T>
    inline Writer& operator<<(T x) {
        if (x < 0)
            pc('-'), x = -x;
        static short tp = 0, s[40];
        do
            s[++tp] = x % 10, x /= 10;
        while (x);
        while (tp)
            pc(s[tp--] + '0');
        return *this;
    }
    inline Writer& operator<<(const char* s) {
        int i = 0;
        while (s[i])
            pc(s[i++]);
        return *this;
    }
    inline Writer& operator<<(char c) {
        pc(c);
        return *this;
    }
    Writer() {}
    ~Writer() { flush(); }
} out;
}  // namespace io
using io::in, io::out;
#define int long long
constexpr int N = 5e5 + 5, N2 = 4e3 + 5;
int n, res;
int qu[N2], vis[N2], dis[N2];
vector<int> g[N2];
pair<int, int> a[N];
atcoder::fenwick_tree<int> val(N), cnt(N);
inline void br() {
    auto adj = [](const auto& a, const auto& b) {
        return abs(a.first - b.first) + abs(a.second - b.second) == 1;
    };
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (adj(a[i], a[j])) {
                g[i].push_back(j);
                g[j].push_back(i);
            }
    auto dij = [](int s) {
        fill(vis + 1, vis + n + 1, 0);
        fill(dis + 1, dis + n + 1, 1e16);
        int h = 0, t = 0;
        qu[h] = s;
        dis[s] = 0;
        while (h <= t) {
            int u = qu[h++];
            for (int v : g[u])
                if (dis[v] > dis[u] + 1) {
                    dis[v] = dis[u] + 1;
                    if (!vis[v]) {
                        vis[v] = 1;
                        qu[++t] = v;
                    }
                }
        }
        return accumulate(dis + 1, dis + n + 1, 0ll);
    };
    int res = 0;
    for (int i = 1; i <= n; ++i)
        res += dij(i);
    assert(res & 1 ^ 1);
    out << (res >> 1);
}
int32_t main(int32_t argc, const char** argv) {
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    in >> n;
    for (int i = 1; i <= n; ++i)
        in >> a[i].first >> a[i].second;
    if (n <= 4000) {
        br();
        return 0;
    }
    for (int i = n; i; --i) {
        int x = a[i].first;
        int v = val.sum(0, x);
        int c = cnt.sum(0, x);
        res += x * c - v;
        v = val.sum(x, N);
        c = cnt.sum(x, N);
        res += v - x * c;
        val.add(x, x);
        cnt.add(x, 1);
    }
    atcoder::fenwick_tree<int> val(N), cnt(N);
    for (int i = n; i; --i) {
        int y = a[i].second;
        int v = val.sum(0, y);
        int c = cnt.sum(0, y);
        res += y * c - v;
        v = val.sum(y, N);
        c = cnt.sum(y, N);
        res += v - y * c;
        val.add(y, y);
        cnt.add(y, 1);
    }
    out << res;
    return 0;
}